package com.ece.pfe_rover_pvt_ines_ece_application.view;

import com.ece.pfe_rover_pvt_ines_ece_application.PFE_ROVER_PVT_INES_ECE_Application;
import com.ece.pfe_rover_pvt_ines_ece_application.R;
import com.parse.ParseAnonymousUtils;
import com.parse.ParseObject;
import com.parse.ParseUser;
import com.parse.ui.ParseLoginBuilder;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class ReferentielActivity extends Activity{
	public boolean DEBUG_MODE = PFE_ROVER_PVT_INES_ECE_Application.DEBUG;
	private static final int LOGIN_ACTIVITY_CODE = 100;
	
	private ActionBar actionBar;

	@Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_referentiel);
	      //Menu & subtitle		
	        actionBar= getActionBar();
			actionBar.setSubtitle(R.string.subtitle_referentiel);
			
		}
	    
	    /**
		 * OnCreateOptionsMenu (Android method related to Activity class)
		 * Inflate the menu
		 */
		@Override
		public boolean onCreateOptionsMenu(Menu menu) {		
			// Inflate the menu
			//this adds items to the action bar if it is present.		
			getMenuInflater().inflate(R.menu.action_logout, menu);
			getMenuInflater().inflate(R.menu.activity_main_actions, menu);		
			getMenuInflater().inflate(R.menu.action_login, menu);							
			
			return true;
		}

		/**
		 * onOptionsItemSelected (Android method related to Activity class)
		 * Start the activity related to a selected field in the menu
		 */
		@Override
		public boolean onOptionsItemSelected(MenuItem item) {		
			// Handle action bar item clicks here. The action bar will
			// automatically handle clicks on the Home/Up button, so long
			// as you specify a parent activity in AndroidManifest.xml.
			int id = item.getItemId();
			switch(id) {
			
			case R.id.measurements_list:
				startActivity(new Intent(this.getBaseContext(),MeasureActivity.class));			
				return true;
			case R.id.measurements_table:
				startActivity(new Intent(this.getBaseContext(),TableActivity.class));			
				return true;
			
			case R.id.measurements_graphs:
				startActivity(new Intent(this.getBaseContext(),GraphsActivity.class));			
				return true;
			
				
			case R.id.action_logout:			
				// Log out the current user
				ParseUser.logOut();
				// Create a new anonymous user
				ParseAnonymousUtils.logIn(null);
							
				// Unpin all the current objects
				ParseObject
						.unpinAllInBackground(PFE_ROVER_PVT_INES_ECE_Application.MEASUREMENTS_GROUP_NAME);
				
				//End the activity & go back the the previous one
				finish();
				
				return super.onOptionsItemSelected(item);
				
			case R.id.action_login:
				invalidateOptionsMenu();
				ParseLoginBuilder builder = new ParseLoginBuilder(this);
				startActivityForResult(builder.build(), LOGIN_ACTIVITY_CODE);
				
				return super.onOptionsItemSelected(item);
			
			default:			
					return super.onOptionsItemSelected(item);
			}
		}
		
		/**
		 * onPrepareOptionsMenu
		 */
		@Override
		public boolean onPrepareOptionsMenu(Menu menu) {		
			
			if(DEBUG_MODE) System.out.println("---- Entering onPrepareOptopnsMenu method...");
			
			boolean realUser;
			super.onPrepareOptionsMenu(menu);
			if(ParseUser.getCurrentUser() != null){
				realUser = !ParseAnonymousUtils.isLinked(ParseUser
					.getCurrentUser());
			
				menu.findItem(R.id.action_login).setVisible(!realUser);						
				
				//Only registered user can see these buttons
				menu.findItem(R.id.action_logout).setVisible(realUser);	
				menu.findItem(R.id.measurements_list).setVisible(realUser);
				menu.findItem(R.id.measurements_table).setVisible(realUser);
				menu.findItem(R.id.measurements_graphs).setVisible(true);
			}	
				menu.findItem(R.id.referentiel).setVisible(false);
			return true;
		}

}
